package com.woniuxy.email.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yellow Docter
 * @date 2024-10-24
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Configuration
public class RabbitMQConfigDead {

    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("normal.queue")
                .deadLetterExchange("dlx.exchange")       //死信交换机绑定
                .deadLetterRoutingKey("dlx.gz")  //死信路由键
                .ttl(20000)   //设置消息过期时间为20秒，消息20秒还没有被消费就进入到死信队列
                .build();
    }

    //正常交换机
    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normal.exchange");
    }

    //正常队列绑定到正常交换机，使用路由键city.gz
    @Bean
    public Binding normalQueueToDirectExchange(Queue normalQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }


    //死信交换机
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange("dlx.exchange");
    }

    /**
     * 创建死信队列、死信交换机、死信队列绑定到交换机
     */
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx.queue").build();
    }

    @Bean
    public Binding deadBinding(Queue dlxQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(dlxQueue).to(deadExchange).with("dlx.gz");
    }

}
